package com.caigou.modules.website.ui

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import androidx.fragment.app.viewModels
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.openBrowser
import com.caigou.comm.base.ext.showToast
import com.caigou.modules.website.R
import com.caigou.modules.website.adpter.WebsiteAdapter
import com.caigou.modules.website.adpter.WebsiteWithPicAdapter
import com.caigou.modules.website.bean.WebSiteWithoutPic
import com.caigou.modules.website.bean.WebsiteBean
import com.caigou.modules.website.bean.WebsiteWithPic
import com.caigou.modules.website.databinding.FragmentWebsiteBinding
import com.caigou.modules.website.viewmodel.WebsiteViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/09/17
 */
@AndroidEntryPoint
class WebsiteFragment : BaseFragment() {
    private var _binding: FragmentWebsiteBinding? = null
    private val binding get() = _binding!!

    private val websiteViewModel: WebsiteViewModel by viewModels()

    private val data1 = mutableListOf<WebsiteWithPic>()
    private val data2 = mutableListOf<WebSiteWithoutPic>()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentWebsiteBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun onBack() {
    }

    override fun initView() {
        binding.websiteSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                websiteViewModel.getWebsite()
            }
        }
    }

    override fun lazyLoad() {
        websiteViewModel.getWebsite()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(websiteViewModel.websiteLiveData, ::handleWebsite)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun handleWebsite(result: Resource<MutableList<WebsiteBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.websiteSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取网站大全信息异常")
                }
            }
            else -> {
                mDialog.dismiss()
                binding.websiteSwipeRefreshLayout.isRefreshing = false
                data1.clear()
                data2.clear()
                result.data?.apply {
                    forEach { website ->
                        if (TextUtils.isEmpty(website.logo)) {
                            data2.add(WebSiteWithoutPic(website.title, website.url))
                        } else {
                            data1.add(WebsiteWithPic(website.title, website.url, website.logo))
                        }
                    }
                    binding.websiteGvHeader.run {
                        adapter = WebsiteWithPicAdapter(requireActivity(), data1)
                        onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
                            openBrowser(data1[position].url)
                        }
                    }
                    binding.websiteGvFooter.run {
                        adapter = WebsiteAdapter(requireActivity(), data2)
                        onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
                            openBrowser(data2[position].url)
                        }
                    }
                }
            }
        }
    }
}